<?php
/**
 * Created by PhpStorm.
 * User: lyc
 * Date: 16/3/9
 * Time: 下午1:24
 */
namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Auth;
use Illuminate\Http\Response;
use PHPExcel;
use PHPExcel_IOFactory;
use DB;
use Hash;
use Illuminate\Support\Str;
use Cache;

include_once dirname(__FILE__) . '/../userdata.php';
$userallclass = new \UserData();

class UserController extends Controller
{
    public function register(Request $request)
    {
        $username = $request->input('username');
        $password = $request->input('password');

        return response()->json(['name' => $username, 'password' => $password]);
    }

    public function login(Request $request)
    {
        $username = $request->input('username');
        $password = $request->input('password');
        return response()->json(['username' => $username,'password' => $password]);
    }


    /**
     * 处理登录认证
     *
     * @return Response
     */
    public function authenticate(Request $request)
    {
        $stuid = $request->input('stuid');
        $password = $request->input('password');

        $filename = public_path() . "/upload_files/score.xlsx";
        $objExcelReader = PHPExcel_IOFactory::createReader("Excel2007");
        $objPHPExcel = $objExcelReader->load($filename);
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();           //取得总行数
        $highestColumn = $sheet->getHighestColumn(); //取得总列数
        $objPHPExcel->setActiveSheetIndex(0);
        $date = $objPHPExcel->getActiveSheet()->getCell('A16')->getValue();
        $i = 0;
        $expiresAt = Carbon::now()->addMinutes(100);
        $objWorksheet = $objPHPExcel->getActiveSheet();

        $userall = array();
        if($userall == null)
        {
            echo 'null';
        }
        foreach ($objWorksheet->getRowIterator() as $row)
        {
            if ($i > 0) {
                $j = 0;
                $cellIterator = $row->getCellIterator();
                $cellIterator->setIterateOnlyExistingCells(false);
                foreach ($cellIterator as $cell) {
                    if($j == 0) {
                        $userdata = array("stuid" => $cell->getValue());
                        $j++;
                    }else if($j == 1) {
                        $userdata['name'] = $cell->getValue();
                        $j++;
                    }else if($j == 2) {
                        $userdata['password'] = Hash::make($cell->getValue());
                        $userdata['remember_token'] = Str::random(60);
                        $j++;
                    }
//                    if($j==3 && $userdata['stuid']!=null)
//                    {
//                            DB::table('users')->insert(
//                                $userdata
//                            );
//                    }

                }
                //Cache::add($userdata['name'],$userdata,$expiresAt);
                if(Cache::has($userdata['name']))
                {
                    echo 'has';
                }
                //var_dump($userdata);
                $userall[$i] = $userdata;

                //return response()->json(['state'=>$cell,'email' => $email,'password' => $password]);
            }
            $i++;

        }
        var_dump($userall);
//        $userallclass->setUserdata($userall);
//        var_dump($userallclass->getUserdata());



//        $objReader = PHPExcel_IOFactory::createReaderForFile($filename);
//        $objPHPExcel = $objReader->load($filename);
//        $objPHPExcel->setActiveSheetIndex(1);
//        $date = $objPHPExcel->getActiveSheet()->getCell('A16')->getValue();
//        DB::table('users')->insert(
//                               array('stuid'=>'211314050','name'=>'JHon','password'=>Hash::make('211314050'))
//        );

        $remember = true;
        echo $remember;
        if (Auth::viaRemember()) {
            echo "via";
        }

        if (Auth::attempt(['stuid' => $stuid, 'password' => $password], $remember)) {
            // 认证通过...
            $user = Auth::user();
            if (Auth::check()) {
                // The user is logged in...
                echo "logging";
                Auth::logout();
            }
            return response()->json(['state'=>'success','email' => $stuid,'password' => $password,'user'=>$user]);
        }
        else{
            $user = Auth::user();
            return response()->json(['state'=>'failed','email' => $stuid,'password' => $password,'user'=>$user]);
        }
    }

    public function updateProfile(Request $request)
    {
        if ($request->user()) {
            return response()->json(['user'=>$request->user()]);
        }
    }

    public function logout(Request $request)
    {
        Auth::logout();
        echo Auth::check();
        return response()->json(['user'=>Auth::check()]);
    }
}
